﻿#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
//转移表
int add(int x, int y)
{
	return x + y;
}
int sub(int x, int y)
{
	return x - y;
}
int mul(int x, int y)
{
	return x * y;
}
int div(int x, int y)
{
	return x / y;
}
void menu()
{
	printf("          转移表           \n");
	printf("***************************\n");
	printf("*********  1.add  *********\n");
	printf("*********  2.sub  *********\n");
	printf("*********  3.mul  *********\n");
	printf("*********  4.div  *********\n");
	printf("*********  0.exit *********\n");
	printf("***************************\n");
}
int main()
{
  //这个函数太过于冗余
	int x = 0;
	int y = 0;
	int ret = 0;
	int imput = 1;
	do
	{
		menu();
		printf("请输入你要操作的选项->");
		scanf("%d", &imput);
		switch (imput)
		{
		case 1:
			printf("输入操作数->");
			scanf("%d %d", &x, &y);
			ret = add(x, y);
			printf("%d\n", ret);
			break;
		case 2:
			printf("输入操作数->");
			scanf("%d %d", &x, &y);
			ret = sub(x, y);
			printf("%d\n", ret);
			break;
		case 3:
			printf("输入操作数->");
			scanf("%d %d", &x, &y);
			ret =mul(x, y);
			printf("%d\n", ret);
			break;
		case 4:
			printf("输入操作数->");
			scanf("%d %d", &x, &y);
			ret = div(x, y);
			printf("%d\n", ret);
			break;
		case 0:
			printf("退出转移表");
		default:
			printf("输入错误，重新输入");
		}
	} while (imput);
	return 0;
}
//
 

//使⽤函数指针数组的实现转移表
int add(int x, int y)
{
	return x + y;
}
int sub(int x, int y)
{
	return x - y;
}
int mul(int x, int y)
{
	return x * y;
}
int div(int x, int y)
{
	return x / y;
}
void menu()
{
	printf("          转移表           \n");
	printf("***************************\n");
	printf("*********  1.add  *********\n");
	printf("*********  2.sub  *********\n");
	printf("*********  3.mul  *********\n");
	printf("*********  4.div  *********\n");
	printf("*********  0.exit *********\n");
	printf("***************************\n");
}
int main()
{
	int x = 0;
	int y = 0;
	int ret = 0;
	int imput = 1;
	int (*p[5])(int x, int y) = { 0,add,sub,mul,div };
	do
	{
		menu();
		printf("请输入你要操作的选项->");
		scanf("%d", &imput);
		if (imput <= 4 && imput >= 1)
		{
			printf("输入操作数->");
			scanf("%d %d", &x, &y);
			int ret = (*p[imput])(x, y);
			printf("%d\n", ret);
		}
		else if (imput == 0)
		{
			printf("退出转移表");
		}
		else
		{
			printf("输入错误，重新输入");
		}
	} while (imput);
	return 0;
}
//

//使用回旋函数实现转移表
int add(int x, int y)
{
	return x + y;
}
int sub(int x, int y)
{
	return x - y;
}
int mul(int x, int y)
{
	return x * y;
}
int div(int x, int y)
{
	return x / y;
}
void cala(int (*pf)(int,int))
{
	int x = 0;
	int y = 0;
	int ret = 0;
	printf("输入操作数->");
	scanf("%d %d", &x, &y);
	ret = pf(x, y);
	printf("%d\n", ret);
}
void menu()
{
	printf("          转移表           \n");
	printf("***************************\n");
	printf("*********  1.add  *********\n");
	printf("*********  2.sub  *********\n");
	printf("*********  3.mul  *********\n");
	printf("*********  4.div  *********\n");
	printf("*********  0.exit *********\n");
	printf("***************************\n");
}

int main()
{
	int imput = 0;
	do
	{
		menu();
		printf("请输入你要操作的选项->");
		scanf("%d", &imput);
		switch (imput)
		{
		case 1:
			cala(add);
			break;
		case 2:
			cala(sub);
			break;
		case 3:
			cala(mul);
			break;
		case 4:
			cala(div);
			break;
		case 0:
			printf("退出转移表");
			break;
		default:
			printf("输入错误，重新输入");
			break;
		}

	} while (imput);
	return 0;
}